//
//  XSViewController.m
//  绿地管理系统
//
//  Created by hexs on 16/1/19.
//  Copyright © 2016年 hexs. All rights reserved.
//

#import "XSViewController.h"

@interface XSViewController ()

@property (nonatomic, strong) UIViewController  *mainViewController;
@property (nonatomic, strong) UIViewController  *leftViewController;
@property (nonatomic, strong) UIView            *maskView;
@property (nonatomic, strong) UIScreenEdgePanGestureRecognizer  *openGesture;


@end

@implementation XSViewController

- (instancetype)initWithMainViewController:(UIViewController *)mainViewController leftViewController:(UIViewController *)leftViewController
{
    self = [super init];
    if (self) {
        self.mainViewController = mainViewController;
        self.leftViewController = leftViewController;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initializeAppearance];
}

- (void)initializeAppearance
{
    [self.view addGestureRecognizer:self.openGesture];
    
    [self addChildViewController:self.leftViewController];
    self.leftViewController.view.frame = CGRectMake(-CGRectGetWidth(self.view.bounds)/2, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds));
    [self.leftViewController didMoveToParentViewController:self];
    
    [self addChildViewController:self.mainViewController];
    [self.view addSubview:self.mainViewController.view];
    [self.mainViewController didMoveToParentViewController:self];
}

#pragma mark -- Method

- (void)openLeftDrawer
{
    [self handleDrawerWithFlag:YES];
}

- (void)closeLeftDrawer
{
    [self handleDrawerWithFlag:NO];
}

// 线性插值
- (CGFloat)interpolateFrom:(CGFloat)from to:(CGFloat)to percent:(CGFloat)percent
{
    return from + (to - from) * percent;
}

- (void)handleDrawerWithFlag:(BOOL)isOpen
{
    CGFloat width = CGRectGetWidth(self.view.frame);
    CGFloat height = CGRectGetHeight(self.view.frame);
    CGFloat maskToAlpha;
    
    CGRect mainToFrame;
    CGRect leftToFrame;
    
    if (isOpen) {
        [self.view addSubview:self.maskView];
        [self.view insertSubview:self.leftViewController.view atIndex:0];
        leftToFrame = self.view.bounds;
        mainToFrame = CGRectMake(width - 82, 0, width, height);
        maskToAlpha = 0.8;
    } else {
        leftToFrame = CGRectMake(-width/2, 0, width, height);
        mainToFrame = self.view.bounds;
        maskToAlpha = 0;
    }
    
    [UIView animateWithDuration:0.65 animations:^{
        self.leftViewController.view.frame = leftToFrame;
        self.mainViewController.view.frame = mainToFrame;
        
        self.maskView.frame = mainToFrame;
        self.maskView.alpha = maskToAlpha;
    } completion:^(BOOL finished) {
        
    }];
}
#pragma mark -- callback
- (void)tapOnMask:(UITapGestureRecognizer *)sender
{
    [self closeLeftDrawer];
}

- (void)onOpenGesture:(UIScreenEdgePanGestureRecognizer *)sender
{
    if (sender.state == UIGestureRecognizerStateBegan) {
        [self.view insertSubview:self.leftViewController.view atIndex:0];
        
        [self.view addSubview:self.maskView];
    } else if (sender.state == UIGestureRecognizerStateChanged) {
        // 改变几个视图的frame 以及 alpha
        // 手指一共只能滑300的距离
        CGFloat maxTranslation = 250.f;
        // 手指的位移
        CGPoint translation = [sender translationInView:sender.view];
        if (translation.x < 0) {
            translation.x = 0;
        }
        CGFloat percent = translation.x / maxTranslation;
        // 如果拖出去了，就当拖到最终的状态来处理
        if (percent > 1) {
            percent = 1;
        }
        CGFloat fromAlpha = 0;
        CGFloat toAlpha = 0.6;
        CGFloat alpha = [self interpolateFrom:fromAlpha to:toAlpha percent:percent];
        self.maskView.alpha = alpha;
        
        
        CGFloat width = CGRectGetWidth(self.mainViewController.view.bounds);
        CGFloat height = CGRectGetHeight(self.mainViewController.view.bounds);
        CGFloat mainFromX = 0;
        CGFloat mainToX =width - 82;
        // 通过线性插值计算当手指移动了 translation后，mainView应该移动到哪里
        CGFloat x = [self interpolateFrom:mainFromX to:mainToX percent:percent];
        self.mainViewController.view.frame = CGRectMake(x, 0, width, height);
        // mask跟mainView是一起的
        self.maskView.frame = self.mainViewController.view.frame;
        
        
        CGFloat leftFromX = -width / 2;
        CGFloat leftToX = 0;
        CGFloat leftX = [self interpolateFrom:leftFromX to:leftToX percent:percent];
        self.leftViewController.view.frame = CGRectMake(leftX, 0, width, height);
    } else if (sender.state == UIGestureRecognizerStateCancelled || sender.state == UIGestureRecognizerStateEnded) {
        CGPoint velocity = [sender velocityInView:self.view];
        if (velocity.x > 0) {
            [self openLeftDrawer];
        } else {
            [self closeLeftDrawer];
        }
    }
}

#pragma mark -- Getter
- (UIView *)maskView
{
    if (_maskView == nil) {
        _maskView = [[UIView alloc] initWithFrame:self.view.bounds];
        [_maskView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapOnMask:)]];
        _maskView.backgroundColor = [UIColor blackColor];
        _maskView.alpha = 0;
    }
    return _maskView;
}

- (UIScreenEdgePanGestureRecognizer *)openGesture
{
    if (_openGesture == nil) {
        _openGesture = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(onOpenGesture:)];
        _openGesture.edges = UIRectEdgeLeft;
    }
    return _openGesture;
}

@end









